/*
 * main.cpp - entry point for program
 * Yet Another Sudoku Solver 
 * CSCE 411
 * by Michael Atkinson
 */

#include "common.h"

char trans[9] = {'Q','R','S','T','U','V','W','X','Y'};

int solve(vector<vector<char> > &puzzle, set<char> &trtable, unsigned int r, unsigned int c);

int main(int argc, char **argv)
{
	// check arguments
	if(argc != 2)
	{
		cerr << "Usage: sudoku [input file]" << endl;
		exit(1);
	}

	// read in file and construct puzzle
	ifstream in;
	string line;
	int pzsize;
	vector<vector<char> > puzzle;
	set<char> trtable;

	in.open(argv[1], ios::in);
	if(!(in.is_open()))
	{
		cerr << "Error opening file: " << argv[1] << endl;
		exit(1);
	}

	getline(in, line);
	pzsize = line.length();
	puzzle.resize(pzsize);

	for(int i = 0; i < pzsize; i++)
	{
		for(int j = 0; j < pzsize; j++)
		{
			if(pzsize > 9 && isdigit(line[j]))
				puzzle[i].push_back(trans[line[j]]);
			else
				puzzle[i].push_back(toupper(line[j]));
		}
		getline(in, line);
	}

	for(int i = 0; i < pzsize; i++)
		for(int j = 0; j < pzsize; j++)
			if(puzzle[i][j] != '.')
				trtable.insert(puzzle[i][j]);

	int val = 'A';
	while(trtable.size() < puzzle.size())
		trtable.insert(val++);

	// solve
	solve(puzzle, trtable, 0, 0);

	// print solution
	for(int i = 0; i < pzsize; i++)
	{
		for(int j = 0; j < pzsize; j++)
			cerr << puzzle[i][j];
		cerr << endl;
	}

	return 0;
}